$(function(){
	var roleDatagrid = $("#role_datagrid");
	var roleDialog = $("#role_dialog");
	var roleDialogForm = $("#role_dialog_form");
	var roleDatagridEditBtn = $("#role_datagrid_bt a");
	var selfPermission = $("#selfPermission");
	var allPermission = $("#allPermission");
	roleDatagrid.datagrid({
		url:"/role_list",
		fit:true,
		pagination:true,
		rownumbers:true,
		singleSelect:true,
		columns:[[
			{title:"角色名称",field:"name",width:1},
			{title:"角色编码",field:"sn",width:1},
		]],
		fitColumns:true,
		toolbar:"#role_datagrid_bt",
		pageList:[1,5,10,20]
	});
	roleDialog.dialog({
		width:600,
		height:400,
		buttons:"#role_dialog_bb",
		modal:true,
		closed:true
	});
	$("#role_department_select").combobox({
		url:"/department_list",
		valueField:'id',    
	    textField:'name' 
	});
	selfPermission.datagrid({
		width:250,
		height:250,
		rownumbers:true,
		singleSelect:true,
		fitColumns:true,
		columns:[[
			{title:"权限名称",field:"name",width:1,align:"center"}
		]],
		onDblClickRow:function(index,row){
			selfPermission.datagrid("deleteRow",index);
		}
	});
	allPermission.datagrid({
		url:"/permission_list",
		width:250,
		height:250,
		pagination:true,
		rownumbers:true,
		singleSelect:true,
		fitColumns:true,
		columns:[[
			{title:"权限名称",field:"name",width:1,align:"center"}
		]],
		onDblClickRow:function(index,row){
			
			var selfRows = selfPermission.datagrid("getRows");
			var flag = false;
			var index = -1;
			for (var i = 0; i < selfRows.length; i++) {
				if(selfRows[i].id==row.id){
					flag = true;
					index = i;
					break;
				}
			}
			if(flag){
				selfPermission.datagrid("selectRow",index);
			}else{
				selfPermission.datagrid("appendRow",row);
			}
		}
	});
	var pager = allPermission.datagrid("getPager");
	pager.pagination({
		showPageList:false,
		showRefresh:false,
		displayMsg:''
	});
	var cmdObj={
			add:function(){
				roleDialog.dialog("open").dialog("setTitle","新增");
				$("[name='id'],[name='name'],[name='sn']").val("");
				selfPermission.datagrid("loadData",{rows:[]})
			},
			edit:function(){
				var rowData = roleDatagrid.datagrid("getSelected");
				if(rowData){
					roleDialog.dialog("open").dialog("setTitle","修改");
					roleDialogForm.form("clear");
					if(rowData.dept){
						rowData["dept.id"]=rowData.dept.id;
					}
					roleDialogForm.form("load",rowData);
					selfPermission.datagrid("options").url = "/permission_selectByRid";
					selfPermission.datagrid("load",{
						rid:rowData.id
					});
				}else{
					$.messager.alert("温馨提示","请选择一行要修改的数据","info");
				}
			},
			del:function(){
				var rowData = roleDatagrid.datagrid("getSelected");
				if(rowData){
					$.messager.confirm("确认对话框","您确定要离职该名员吗？",function(r){
						if(r){
							$.get("/role_delete?id="+rowData.id,function(data){
								if(data.success){
									$.messager.alert("温馨提示",data.msg,"info",function(){
										roleDatagrid.datagrid("load");
									});
								}else{
									$.messager.alert("温馨提示",data.msg,"warning");
								}
							},"json")
						}
					});
				}else{
					$.messager.alert("温馨提示","请选择一行要离职的员工数据","info");
				}
				
			},
			refresh:function (){
				roleDatagrid.datagrid("reload");
			},
			cancel:function (){
				roleDialog.dialog("close");
			},
			save:function (){
				var id = $("input[name='id']").val();
				var url;
				if(id){
					url="/role_update";
				}else{
					url="/role_save";
				}
				roleDialogForm.form("submit",{
					url:url,
					onSubmit:function(param){
						var selfRows = selfPermission.datagrid("getRows");
						for (var i = 0; i < selfRows.length; i++) {
							param["permissions["+i+"].id"]=selfRows[i].id;
						}
					},
				    success:function(data){    
				        data = $.parseJSON(data);
				        if(data.success){
				        	$.messager.alert("温馨提示",data.msg,"info",function(){
				        		roleDialog.dialog("close");
				        		roleDatagrid.datagrid("load");
				        	});
				        }else{
				        	$.messager.alert("温馨提示",data.msg,"warning");
				        }
				    } 
				});
			},
			searchContent:function (){
				var arr = $("#role_search_form").serializeArray();
				var param = {};
				for(var i=0;i<arr.length;i++){
					param[arr[i].name]=arr[i].value;
				}
				roleDatagrid.datagrid("load",param);
			}
	}
	$("a[data-cmd]").on("click",function(){
		var cmd = $(this).data("cmd");
		if(cmd){
			cmdObj[cmd]();
		}
	});
});
function stateFormatter(value,row,index){
	if(value){
		return "<font color='green'>在职</font>"
	}else{
		return "<font color='red'>离职</font>"
	}
}
